package com.pilipili.zeus.eduService.controller;


import com.pilipili.commonutils.Result;
import com.pilipili.zeus.eduService.entity.EduChapter;
import com.pilipili.zeus.eduService.entity.chapter.Chapter;
import com.pilipili.zeus.eduService.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 番剧 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/eduService/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "根据章节id获取视频")
    @GetMapping("getChapter/{courseId}")
    public Result getChapter(@PathVariable String courseId){
        List<Chapter> videos = chapterService.getChapterVideoById(courseId);
        return Result.ok().data("videos",videos);
    }
    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return Result.ok();
    }
    @ApiOperation(value = "根据章节id获取")
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter",eduChapter);
    }
    @ApiOperation(value = "更新章节")
    @PostMapping("updateChapterInfo")
    public Result updateChapterInfo(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return Result.ok();
    }
    @ApiOperation(value = "删除章节")
    @DeleteMapping("removeChapter/{chapterId}")
    public Result removeChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteById(chapterId);
        return flag?Result.ok():Result.error();
    }
    @ApiOperation(value = "读取courseSortExcel中的表格,写入数据库")
    @PostMapping("addChapters")
    public Result addChapters(MultipartFile file){
        chapterService.saveChaptersDataByExcel(file,chapterService);
        return Result.ok();
    }
}

